package net.starwifi.duobao.utils;

import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/*
import cn.com.zjtelecom.smgp.Client;
import cn.com.zjtelecom.smgp.bean.Result;
import cn.com.zjtelecom.smgp.bean.Submit;
*/


/**
 * Created by Administrator on 2015/9/1.
 */
public class SmsUtils {



    public static void sendSms(String mobile,String code,String tpleId){
        Map<String,Object> params=new HashMap<String, Object>();
        String key ="1b906973be874805b22bb308beae4296";
        params.put("mobile",mobile);
        String msg="#code#="+code;
        params.put("tpl_id",tpleId);
        params.put("tpl_value",msg);
        params.put("key",key);
        String baseUrl="http://apis.haoservice.com/sms/send";
        String  resultMsg= HttpUtils.get(baseUrl,params);
        JSONObject json =  JSONObject.parseObject(resultMsg);
        String msge = json.get("error_code").toString();

       /* String host = "115.168.80.52";
        String account = "handdx";
        String passwd = "1065923";
        String spid = "20006";
        String spnum ="1065923000005";
        int port = 8891;
        String destnum = mobile;
        Client client = new Client(host, port, 2, account, passwd, spid, 0);
        Submit submit = new Submit();
        submit.setSrcTermid(spnum);
        submit.setDestTermid(destnum);
        try {
            submit.setMsgContent(content.getBytes("iso-10646-ucs-2"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        submit.setMsgFormat(8);
        Result result = client.Send(submit);
        System.out.println("Status:" + result.ErrorCode);
        System.out.println("MsgID:" + result.ErrorDescription);
        client.Close();*/

    }


    public static void sendlttSms(String mobile,String goodCode,String goodName,String tpleId){
        Map<String,Object> params=new HashMap<String, Object>();
        String key ="1b906973be874805b22bb308beae4296";
        params.put("mobile",mobile);
        String msg="#goodCode#="+goodCode+"&#goodName#="+goodName;
        params.put("tpl_id",tpleId);
        params.put("tpl_value",msg);
        params.put("key",key);
        String baseUrl="http://apis.haoservice.com/sms/send";
        String  resultMsg= HttpUtils.get(baseUrl,params);
        JSONObject json =  JSONObject.parseObject(resultMsg);
        String msge = json.get("error_code").toString();

       /* String host = "115.168.80.52";
        String account = "handdx";
        String passwd = "1065923";
        String spid = "20006";
        String spnum ="1065923000005";
        int port = 8891;
        String destnum = mobile;
        Client client = new Client(host, port, 2, account, passwd, spid, 0);
        Submit submit = new Submit();
        submit.setSrcTermid(spnum);
        submit.setDestTermid(destnum);
        try {
            submit.setMsgContent(content.getBytes("iso-10646-ucs-2"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        submit.setMsgFormat(8);
        Result result = client.Send(submit);
        System.out.println("Status:" + result.ErrorCode);
        System.out.println("MsgID:" + result.ErrorDescription);
        client.Close();*/

    }

    /**
     * 创建内容
     * */
    public static String createSMSContent(String content,Map<String,String> params){
        for(String key:params.keySet()){
            content=content.replace(key,params.get(key));
        }
        return content;
    }



    /** 生成数字验证码*/
    public static String getRandomNumber(int len){
        String content="";
        for(int i=0;i<len;i++){
            content=content+ new Random().nextInt(10);
        }
        return content;
    }

    public static void main(String[ ] arg){
        String mobile="15872610102";
        String goodCode="20160602145554";
        String goodName="Apple iphone6s(16G金色)";
        String tpleId="1730";

    sendlttSms(mobile,goodCode,goodName,tpleId);
    }

}
